
//
//  TabBarViewController.m
//  Demo1
//
//  Created by 胡旭辉 on 2019/7/2.
//  Copyright © 2019 胡旭辉. All rights reserved.
//

#import "TabBarViewController.h"
#import "LoginViewController.h"
#import "DiscoverListViewController.h"
#import "NavigationViewController.h"
#import "HomePageMainListViewController.h"
#import "LessonPageViewController.h"
#import "MinePageViewController.h"
#import "UITabBar+badge.h"
#import "UITabBarController+HXHTabBarController.h"
@interface TabBarViewController ()
@property (nonatomic,strong)NSMutableArray *vcs;
@property (nonatomic,strong)NSMutableArray *arr;
@end

@implementation TabBarViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}

- (void)setUI{
    
    NSMutableDictionary *attrs            = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName]            = [UIFont systemFontOfSize:12];
    attrs[NSForegroundColorAttributeName] = RGBA(136, 141, 166, 1);

    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = RGBA(36, 39, 51, 1);
    selectedAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    UITabBarItem *item = [UITabBarItem appearance];

    if (@available(iOS 10.0, *)) {
        [[UITabBar appearance]setUnselectedItemTintColor:RGBA(136, 141, 166, 1)];
        [[UITabBar appearance]setTintColor:RGBA(36, 39, 51, 1)];
    } else{
        [item setTitleTextAttributes:attrs         forState:UIControlStateNormal];
        [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    }

    [[UITabBar appearance]setBarTintColor:UIColor.whiteColor];
    
    [self setupChildVC:[[DiscoverListViewController alloc]init] title:@"首页" image:@"common_home_tag" seletedImage:@"common_home_select_tag"];
    [self setupChildVC:[[HomePageMainListViewController alloc]init] title:@"发现" image:@"common_discover_tag" seletedImage:@"common_discover_select_tag"];
    [self setupChildVC:[[LessonPageViewController alloc]init] title:@"公益课程" image:@"common_course_tag" seletedImage:@"common_course_select_tag"];
    
    MinePageViewController *mineVC = [[MinePageViewController alloc]init];
    
    [self setupChildVC:mineVC title:@"我的" image:@"common_profile_tag" seletedImage:@"common_profile_selected_tag"];

    
    [self addObserver:self forKeyPath:@"selectedViewController" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if([keyPath isEqualToString:@"selectedViewController"]){
        if(nil != change[@"old"]) {//记录前一个tab}
           
            NSInteger lastIndex = [self.viewControllers indexOfObject:change[@"old"]];
//            NSInteger = index = self.

            NavigationViewController *nav = change[@"new"];
            if (nav.viewControllers.count != 0 &&
                [nav.viewControllers.firstObject isKindOfClass:[MinePageViewController class]] &&
                ![RPAppManager isLogin]) {
                // 发送通知 关闭定时器
                [[NSNotificationCenter defaultCenter] postNotificationName:@"removeTimer" object:nil];
                [[NSUserDefaults standardUserDefaults] setObject:@(lastIndex) forKey:@"tabItemIndex"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                [MGJRouter openURL:@"local://ddup/unLogin" withUserInfo:@{@"navigationVC":nav} completion:nil];
            
                
            }
        }
    }
}

-(void)setupChildVC:(UIViewController *)vc title:(NSString *)title image:(NSString *)image seletedImage:(NSString *)seletedImage
{
    NavigationViewController *nav = [[NavigationViewController alloc] initWithRootViewController:vc];
    nav.tabBarItem.image         = [UIImage imageNamed:image];
    nav.tabBarItem.selectedImage = [UIImage imageNamed:seletedImage];
    nav.tabBarItem.image = [nav.tabBarItem.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav.tabBarItem.selectedImage = [nav.tabBarItem.selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav.tabBarItem.title         = title;
  
    nav.view.backgroundColor     = [UIColor whiteColor];
    [self addChildViewController:nav];
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{

    if ([item.title isEqualToString:@"我的"]) {
        [MobClick event:@"tabbar_mine_click"];
    }

    if ([item.title isEqualToString:@"公益课程"]) {
        [MobClick event:@"tabbar_lesson_click"];
    }
    
}

- (void)dealloc{
    [self removeObserver:self forKeyPath:@"selectedViewController"];
}


@end
